package com.woyao;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;

import com.woyao.core.model.GetMyMemberResponse;
import com.woyao.core.model.MemberSummary;
import com.woyao.core.service.AccountService;
import com.woyao.core.util.ServiceFactory;

import java.io.IOException;
import java.util.List;

import retrofit2.Call;

public class FilterMember extends AppCompatActivity {


    LinearLayoutManager allLayoutManager = null;
    private RecyclerView alllist = null;
    private MemberAdapter allAdapter;

    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_filter_member);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);
        final ActionBar ab = getSupportActionBar();
        //       ab.setHomeAsUpIndicator(R.drawable.icon_menu); // set a custom icon for the default home button
        ab.setDisplayShowHomeEnabled(true); // show or hide the default home button
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDisplayShowCustomEnabled(true); // enable overriding the default toolbar layout
//        ab.setDisplayShowTitleEnabled(false); // disable the default title element


        alllist = (RecyclerView) findViewById(R.id.filter_member_list);
        allLayoutManager = new LinearLayoutManager(this);
        allLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        alllist.setLayoutManager(allLayoutManager);
        alllist.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));

        this.setTitle("选择我的身份");
        
        loadAllData();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        finish();

        return true;
    }




    private void loadAllData(){
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("拼命加载中······");
        progressDialog.show();

        AsyncTask<Void,Void,GetMyMemberResponse> task =
                new AsyncTask<Void, Void, GetMyMemberResponse>() {
                    @Override
                    protected GetMyMemberResponse doInBackground(Void... params) {
                        AccountService svc = ServiceFactory.get(AccountService.class);
                        Call<GetMyMemberResponse> responseCall = svc.getMyMember(WoyaoooApplication.userId);
                        try {
                            GetMyMemberResponse response = responseCall.execute().body();
                            return response;
                        } catch (IOException e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                    @Override
                    protected void onPostExecute(final GetMyMemberResponse response) {
                        progressDialog.dismiss();

                        renderAll(response.getContent());
                    }
                    @Override
                    protected void onCancelled() {
                        progressDialog.dismiss();
                    }
                };
        task.execute();
    }


    private void renderAll( List<MemberSummary> members ){
        MemberSummary ms = new MemberSummary();
        ms.setId(0);
        ms.setTitle( "个人名义");
        members.add(ms);
        allAdapter = new MemberAdapter(this,  members );

        allAdapter.setChangedHandler(new MemberAdapter.Changed() {
            @Override
            public void view(MemberSummary ms) {
                Intent intent = new Intent();
                intent.putExtra("member_id", ms.getId());
                intent.putExtra("member_title", ms.getTitle());
                setResult(666, intent);
                finish();
            }
        });


        this.alllist.setAdapter(allAdapter);

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent();
            setResult(0, intent);
            finish();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

}

